<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="left">
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="物资编号" prop="assetsCode">
						<el-input v-model="form.assetsCode" placeholder="资产编号" maxlength="40" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="报修人" prop="applicant">
						<select-user-dialog v-model:accountId="form.applicant" :mode="false"/>
					</el-form-item>
				</el-col>

			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="维修人" prop="fixedUser">
						<select-user-dialog v-model:accountId="form.fixedUser" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="维修时间" prop="fixedTime">
						<el-date-picker v-model="form.fixedTime" placeholder="维修时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>

			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="问题描述" prop="problemDescription">
						<el-input v-model="form.problemDescription" type="textarea" placeholder="问题描述" maxlength="480" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="维修情况" prop="storageId">
						<el-input v-model="form.details" type="textarea" placeholder="维修情况" maxlength="480" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注说明">
				<el-input v-model="form.remark" type="textarea" placeholder="备注说明" maxlength="180" clearable></el-input>
			</el-form-item>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :data="{module:'fixedattach'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import AttachMenu from "@/views/module/attachMenu";

export default {
	emits: ['success', 'closed'],
	components: {
		selectUserDialog,
		AttachMenu
	},
	data() {
		return {
			apiObj: this.$API.file.attach.uploadFile,
			fileList: [],
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			titleMap: {
				add: '资产维修登记',
				edit: '资产维修对话框',
			},
			form: {
				assetsId: '',
				repairId: '',
				assetsCode: '',
				applicant: '',
				fixedUser: '',
				fixedTime: '',
				problemDescription: '',
				attachId: '',
				remark: '',
				details: '',
			},
			options: [{
				value: '0',
				label: '请选择'
			}],
			rules: {
				assetsName: [
					{required: true, message: '资产名称不能为空', trigger: 'blur'}
				],
				sortId: [
					{required: true, message: '资产分类不能为空', trigger: 'blur'}
				],
				assetsCode: [
					{required: true, message: '资产编号不能为空', trigger: 'blur'}
				],
				unit: [
					{required: true, message: '请选择计量单位', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.office.fixed.insertFixedAssetsRepair.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.office.fixed.updateFixedAssetsRepair.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.assetsId = data.assetsId
			this.form.repairId = data.repairId
			this.form.assetsCode = data.assetsCode
			this.form.applicant = data.applicant
			this.form.fixedUser = data.fixedUser
			this.form.fixedTime = data.fixedTime
			this.form.problemDescription = data.problemDescription
			this.form.attachId = data.attachId
			this.form.remark = data.remark
			this.form.details = data.details

		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		},
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
